package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.acticle.domain.StrategyContent;
import cn.wolfcode.luowowo.acticle.domain.StrategyDetail;
import cn.wolfcode.luowowo.acticle.query.StrategyDetailQueryObject;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import cn.wolfcode.luowowo.acticle.service.IStrategyCommendService;
import cn.wolfcode.luowowo.acticle.service.IStrategyDetailService;
import cn.wolfcode.luowowo.acticle.service.IStrategyTagService;
import cn.wolfcode.luowowo.acticle.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.prefix.StrategyStatisKeyPrefix;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQueryObject;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.ToastsUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 攻略资源管理器
 */
@Controller
@RequestMapping("/strategy")
public class StrategyController {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategySearchService strategySearchService;

    //暂时模拟首页
    @RequestMapping("")
    public String index(Model model){
        //commends 攻略推荐
        model.addAttribute("commends", strategyCommendService.getCommentTop5());

        List<StrategyStatisVO> all = strategyStatisRedisService.getAllStrategyCommends();
        List<StrategyStatisVO> abroadCds = new ArrayList<>();
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();
        for (StrategyStatisVO vo : all) {
            if(vo.isIsabroad()){
                //abroadCds 国外前十攻略推荐
                if(abroadCds.size() < 10){
                    abroadCds.add(vo);
                }
            }else{
                //unabroadCds 国内前十攻略推荐
                if(unabroadCds.size() < 10){
                    unabroadCds.add(vo);
                }
            }
        }
        //abroadCds 国外前十攻略推荐
        model.addAttribute("abroadCds", abroadCds);

        //unabroadCds 国内前十攻略推荐
        model.addAttribute("unabroadCds", unabroadCds);

        //hotCds 前十最热门的攻略推荐
        List<StrategyStatisVO> hots = strategyStatisRedisService.getAllHotStrategyCommends();
        model.addAttribute("hotCds", hots);

        //themeCds 主题推荐          //tc.themeVo       //tc.destVOs
        //List<Map<String, Object>>    map.put("themeVo", themeVo)   map.put("destVos", List<destVo>)
        List<Map<String, Object>> themeCds = strategySearchService.queryThemeCommends();
        model.addAttribute("themeCds", themeCds);


        //chinas 国内攻略 ， 国内按照省份进行统计攻略个数，显示省名
        List<StatisVO> chinas = strategySearchService.queryStrategyByCondition(SearchQueryObject.CONDITION_TYPE_UNABROAD);
        model.addAttribute("chinas", chinas);
        //abroads 国外攻略， 国外按照国家进行统计攻略个数，显示国家名
        List<StatisVO> abroads = strategySearchService.queryStrategyByCondition(SearchQueryObject.CONDITION_TYPE_ABROAD);
        model.addAttribute("abroads", abroads);
        //themes 主题攻略，  根据不同主题进行统计攻略个数，显示主题名
        List<StatisVO> themes = strategySearchService.queryStrategyByCondition(SearchQueryObject.CONDITION_TYPE_THEME);
        model.addAttribute("themes", themes);
        return "strategy/index";
    }

    //攻略明细
    @RequestMapping("/detail")
    public String detail(Long id, Model model) { //id为攻略明细id
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent strategyContent = strategyDetailService.getContent(id);
        detail.setStrategyContent(strategyContent);
        //detail 攻略明细
        model.addAttribute("detail", detail);
        //vo 攻略统计数据(从redis中获取)
        //strategyStatisRedisService.viewIncrease(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO, id);//阅读量增加
        strategyStatisRedisService.numIncrease(id, 0);
        //热门推荐数+1
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT, id, 1);
        model.addAttribute("vo", strategyStatisRedisService.getStatisVo(id));
        return "strategy/detail";
    }
    //攻略明细列表
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQueryObject qo){
        //共享toasts和dest
        ToastsUtil.addAttibuteByDestId(model, destinationService, qo.getDestId());
        //tags
        model.addAttribute("tags", strategyTagService.list());
        //pageInfo
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategy/list";
    }

    //保存评论内容
    @RequestMapping("/commentAdd")
    @ResponseBody
    @RequireLogin
    public Object commentAdd(StrategyComment strategyComment, @UserParam UserInfo userInfo){
        strategyComment.setUserId(userInfo.getId());
        strategyComment.setUsername(userInfo.getNickname());
        strategyComment.setCity(userInfo.getCity());
        strategyComment.setLevel(userInfo.getLevel());
        strategyComment.setHeadUrl(userInfo.getHeadImgUrl());
        strategyCommentService.saveOrUpdate(strategyComment);
        //strategyStatisRedisService.replyIncrease(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO, strategyComment.getDetailId());
        strategyStatisRedisService.numIncrease(strategyComment.getDetailId(), 1);
        //热门推荐数+1
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT, strategyComment.getDetailId(), 1);
        //评论后, 评论数加一
        StrategyStatisVO vo = strategyStatisRedisService.getStatisVo(strategyComment.getDetailId());
        return new AjaxResult().addData(vo.getReplynum());
    }

    //获取评论数据
    @RequestMapping("/comment")
    public String comment(@ModelAttribute StrategyCommentQueryObject qo, Model model){
        //page  //page.content
        //spring-data提供的分类封装对象
        Page<StrategyComment> page = strategyCommentService.query(qo);
        model.addAttribute("page", page);
        return "strategy/commentTpl";
    }

    //点赞
    @ResponseBody
    @RequireLogin
    @RequestMapping("/commentThumbUp")
    public Object commentThumbUp(String toid, Long fromid){
        boolean ret = strategyCommentService.thumbUp(toid, fromid);
        return new AjaxResult(ret, "");
    }

    //收藏
    @ResponseBody
    @RequestMapping("/favor")
    public Object favor(Long sid, @UserParam UserInfo userInfo){
        if(userInfo == null){
            return new AjaxResult("请先登录", AjaxResult.CODE_TYPE_UNLOGIN); //data.code == 102
        }
        boolean ret = strategyStatisRedisService.favor(sid, userInfo.getId());
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT, sid, ret?1:-1);
        return new AjaxResult(ret, "").addData(strategyStatisRedisService.getStatisVo(sid));
    }

    //顶: 即点赞
    @ResponseBody
    @RequestMapping("/strategyThumbup")
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo){
        if(userInfo == null){
            return new AjaxResult("请先登录再顶", AjaxResult.CODE_TYPE_UNLOGIN);
        }
        boolean ret = strategyStatisRedisService.thumbup(sid, userInfo.getId());
        if (ret){
            strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT, sid, 1);
        }
        return new AjaxResult(ret, "").addData(strategyStatisRedisService.getStatisVo(sid));
    }

    //searchPage
    @RequestMapping("/searchPage")
    public String searchPage(Model model, @ModelAttribute("qo") SearchQueryObject qo){
        //page   输入目地点，进行全文搜索
        //page.content
        Page<StrategyTemplate> page = strategySearchService.query(qo);
        model.addAttribute("page", page);
        return "strategy/searchPageTpl";
    }

}
